package cn.ffcs.bpm.web;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.bpm.model.Part;
import cn.ffcs.bpm.model.PartRelaUser;
import cn.ffcs.bpm.service.PartService;
import cn.ffcs.security.model.User;
import cn.ffcs.security.service.UserService;

/**
 * 
 * 角色与用户关联关系 action层，进行业务逻辑操作<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class PartRelaUserAction<R extends PartRelaUser> extends BaseAction<R>
{

    /** 网络标识 */
    private static final long serialVersionUID = -8246546789872993515L;
    /** 业务POJO **/
    private R partRelaUser;
    /** 业务列表数据 **/
    private List<R> partRelaUsers;
    /** 用户ID **/
    private String userID;
    /** 角色服务 **/
    @SuppressWarnings("unchecked")
    private PartService partService;
    /** 角色列表数据 **/
    private List<Part> parts;
    /** 已选择角色 **/
    private String selectedParts;
    /** 用户服务 **/
    @SuppressWarnings("unchecked")
    private UserService userService;
    
    /**
     * 
     * 初始化业务数据
     * @return
     */
    @SuppressWarnings("unchecked")
    public String init(){
        
        //获取所有角色数据
        parts=partService.findAll();
        //获取用户角色数据
        HashMap<String, Object> hmCond=new HashMap<String, Object>();
        hmCond.put("user.id", partRelaUser.getUser().getId());
        setConds(hmCond);
        partRelaUsers=findBy();
        //初始化已选择资源数据
        String selpart="";
        for (PartRelaUser r:partRelaUsers){
            if (StringUtils.isEmpty(selpart)){
                selpart=r.getPart().getId().toString();
            }else{
                selpart+=","+r.getPart().getId().toString();
            }
        }
        selectedParts=selpart;
        //获取实体
        User user=(User)userService.findById(partRelaUser.getUser().getId());
        partRelaUser.setUser(user);
        
        return SUCCESS;  
    }
    
    /**
     * 
     * 保存角色数据
     * @return
     */
    public String savePartRelaUser(){
        //先删除对应角色数据
        baseService.deleteBy(partRelaUser, "user", partRelaUser.getUser().getId().toString());
        
        //数据保存 操作; 
        if (StringUtils.isNotEmpty(selectedParts)){
            String[] parts=selectedParts.split(",");
            for (String partId:parts){
                partRelaUser.setId(null);
                Part part=new Part();
                part.setId(Integer.parseInt(partId));
                partRelaUser.setPart(part);
                baseService.saveOrUpdate(partRelaUser);
            }
        }
        
        //初始化
        init();
        
        return SUCCESS;        
    }
    
    /**
     * 
     * 初始化页面数据
     * @return
     */
    @SuppressWarnings("unchecked")
    public String authPart()
    {
        //获取所有角色数据
        parts=partService.findAll();
        //获取用户角色数据
        partRelaUsers=findBy();
        //获取实体
        User user=(User)userService.findById(partRelaUser.getUser().getId());
        partRelaUser.setUser(user);
        //初始化
        init();
        
        return SUCCESS;
    }
    
    /**
     * @return 用户ID
     */
    public String getUserID()
    {
        return userID;
    }

    /**
     * 设置用户ID
     * @param userID 
     */
    public void setUserID(String userID)
    {
        this.userID = userID;
    }

    /**
     * @return the selectedParts
     */
    public String getSelectedParts()
    {
        return selectedParts;
    }

    /**
     * @param selectedParts the selectedParts to set
     */
    public void setSelectedParts(String selectedParts)
    {
        this.selectedParts = selectedParts;
    }

    /**
     * @return the userService
     */
    public UserService getUserService()
    {
        return userService;
    }

    /**
     * @param userService the userService to set
     */
    public void setUserService(UserService userService)
    {
        this.userService = userService;
    }

    public R getPartRelaUser()
    {
        return partRelaUser;
    }

    public void setPartRelaUser(R partRelaUser)
    {
        this.partRelaUser = partRelaUser;
    }

    public List<R> getPartRelaUsers()
    {
        return partRelaUsers;
    }

    public void setPartRelaUsers(List<R> partRelaUsers)
    {
        this.partRelaUsers = partRelaUsers;
    }

    public List<Part> getParts()
    {
        return parts;
    }

    public void setParts(List<Part> parts)
    {
        this.parts = parts;
    }

    public PartService getPartService()
    {
        return partService;
    }

    public void setPartService(PartService partService)
    {
        this.partService = partService;
    }

}
